package menus;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import data.Database;

import Aplicacion.MP3Player;
import Aplicacion.Variables_globales;

public class MenuFooter {
	private final Database db = new Database();
	private boolean btnPress = false;
	
	private  Dimension sizeLienzo;
	
	private int x = 0;
	private int y = 0;
	
	private BufferedImage footMenu;
	private Graphics2D footMenu_graphics;
	
	public MenuFooter(){
		sizeLienzo = new Dimension(0,0);
	}

	public Graphics paint (Graphics g) {
		if (db.enRed()) {
			Variables_globales.JUGADOR_ACTUAL = db.getJugadorActual();
//			Variables_globales.ESTADO = db.getEstado();
			
			if (db.getJugadorActual() != Variables_globales.JUGADOR_PARTIDA_RED) Variables_globales.PAUSA = true;
			else Variables_globales.PAUSA = false;
			
//			if (Variables_globales.ESTADO != Variables_globales.ESTADOS.MENU) Variables_globales.MOSTRAR_FOOTER = true;
		}
		
		if (Variables_globales.MOSTRAR_FOOTER) {
			footMenu = new BufferedImage(sizeLienzo.width, 30, BufferedImage.TYPE_INT_ARGB);
			footMenu_graphics = (Graphics2D) footMenu.getGraphics();
			footMenu_graphics.setColor(Variables_globales.LISTA_JUGADORES.get(Variables_globales.JUGADOR_ACTUAL).getColor());
			footMenu_graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
			footMenu_graphics.fillRect(0, 0, footMenu.getWidth(), footMenu.getHeight());
			
			footMenu_graphics.setColor(Color.WHITE);
			footMenu_graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
			String nombreJ = Variables_globales.LISTA_JUGADORES.get(Variables_globales.JUGADOR_ACTUAL).getNombre();
			footMenu_graphics.drawString("Jugador Actual: "+nombreJ+", Soldados Disponibles: "+db.getSoldadosDisponibles(), 20, 20);
			
			footMenu_graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
			footMenu_graphics.setColor(Color.DARK_GRAY);
			footMenu_graphics.fillRect((footMenu.getWidth()-250), 2, 100, 25);
			footMenu_graphics.fillRect((footMenu.getWidth()-120), 2, 100, 25);
			footMenu_graphics.setColor(Color.WHITE);
			footMenu_graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
			footMenu_graphics.drawString("Continuar", (footMenu.getWidth()-100), 20);
			footMenu_graphics.drawString("Menu", (footMenu.getWidth()-220), 20);
			
			String tmp = "";
			switch (Variables_globales.ESTADO) {
				case POSICIONAR:
					tmp = "Posicionar";
					break;
				case ATACAR:
					tmp = "Atacar";
					break;
				case DEFENDER:
					tmp = "Defender";
					break;
				case PAUSA:
					tmp = "Pausa";
			}
			footMenu_graphics.setColor(Color.WHITE);
			footMenu_graphics.drawString(tmp, (footMenu.getWidth()-600), 18);
			
			g.drawImage(footMenu, 0, (sizeLienzo.height-50), null);
			
			if (btnPress) this.buttonPressed(); 
			
			if (Variables_globales.PAUSA) {
				Graphics2D g2d = (Graphics2D) g;
				g2d.setColor(Variables_globales.LISTA_JUGADORES.get(Variables_globales.JUGADOR_ACTUAL).getColor());
				g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f));
				g2d.fillRect(0, 0, sizeLienzo.width, sizeLienzo.height);
			}
		}
		return g;
	}
	
	private void buttonPressed () {
		// Boton Continuar
		if (x > (footMenu.getWidth()-120) && x < ((footMenu.getWidth()-120)+100)) {
			if (y > ((sizeLienzo.height-45)) && y < ((sizeLienzo.height-5))) {
				if (Variables_globales.ESTADO == Variables_globales.ESTADOS.POSICIONAR) {
					Variables_globales.ESTADO = Variables_globales.ESTADOS.ATACAR;
					db.setEstado(1);
					
				} else if (Variables_globales.ESTADO == Variables_globales.ESTADOS.ATACAR) {
					Variables_globales.ESTADO = Variables_globales.ESTADOS.DEFENDER;
					db.setEstado(2);
					
				} else if (Variables_globales.ESTADO == Variables_globales.ESTADOS.DEFENDER) {
					if (Variables_globales.JUGADOR_ACTUAL == Variables_globales.LISTA_JUGADORES.size()-1) Variables_globales.JUGADOR_ACTUAL=0;
					else Variables_globales.JUGADOR_ACTUAL++;
					db.setJugadorActual();
					
					if (db.getTurnos() > 2) db.updateSoldadosDisponibles(db.getSoldadosDisponibles()+3);
					
					Variables_globales.ESTADO = Variables_globales.ESTADOS.POSICIONAR;
					db.setEstado(0);
				}
				try {
					MP3Player WAV = new MP3Player();
					WAV.playDaemon(this.getClass().getResource("/Imagenes/click.mp3").toString());
				} catch (IllegalThreadStateException ex) {}
			}
		}
		
		// Boton Menu
		if (x > (footMenu.getWidth()-220) && x < ((footMenu.getWidth()-220)+100)) {
			if (y > ((sizeLienzo.height-45)) && y < ((sizeLienzo.height-5))) {
				Variables_globales.ESTADO = Variables_globales.ESTADOS.MENU;
				
				try {
					MP3Player WAV = new MP3Player();
					WAV.playDaemon(this.getClass().getResource("/Imagenes/click.mp3").toString());
				} catch (IllegalThreadStateException ex) {}
				
				Variables_globales.MOSTRAR_FOOTER = false;
			}
		}
		
		btnPress = false;
	}
	
	public void update (int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void setBtnPress(boolean btnPress) {
		this.btnPress = btnPress;
	}
	
	public void setSize (int w, int h) {
		sizeLienzo	= new Dimension(w, h);
	}
}